package org.trackcore.handler;

import org.trackcore.model.entity.PerformanceLoad;
import org.trackcore.service.PerformanceLoadService;
import org.trackcore.utils.DataUtils;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 处理页面加载性能数据
 * 处理器Key: "performance_load"
 */
@Component
public class PerformanceLoadHandler implements DataHandler {

    private final PerformanceLoadService loadService;

    public PerformanceLoadHandler(PerformanceLoadService loadService) {
        this.loadService = loadService;
    }

    @Override
    public String getHandlerKey() {
        return "performance_load"; // 对应type=performance, subType=load
    }

    @Override
    public void handle(Map<String, Object> data, String logId) {
        PerformanceLoad load = PerformanceLoad.builder()
                .logId(logId)
                .startTime(DataUtils.getDouble(data, "startTime"))
                .pageUrl(DataUtils.getString(data, "pageUrl"))
                .build();

        loadService.save(load);
    }
}
